package pchi.pchicamera.helpers;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class LayoutHelpers {
	public static float getColumnWidth(
			Context context, 
			float minColWidth, 
			float space,
			float leftMargin) {
		float displayWidth = (float)getDeviceWidth(context) - convertToPx(context, leftMargin);
		minColWidth = convertToPx(context, minColWidth);
		space = convertToPx(context, space);
		
		float bestWidth = Float.MAX_VALUE;
		for(int i = 2; ; i++) {
			float totalSpace = (i - 1) * space;
			float tmpWidth = (displayWidth - totalSpace) / i;
			
			if(tmpWidth < minColWidth)
				break;
			
			if(tmpWidth < bestWidth)
				bestWidth = tmpWidth;
		}
		
		return bestWidth;
	}
	
	public static int getDeviceWidth(Context context) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return metrics.widthPixels;
	}
	
	public static float convertToPx(Context context, float dip) {
		Resources r = context.getResources();
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
	}
}
